package yang.cn.com.greendaoapp.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tapadoo.alerter.Alerter;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yang.cn.com.greendaoapp.R;
import yang.cn.com.greendaoapp.SDCardActivity;
import yang.cn.com.greendaoapp.base.BaseFragment;
import yang.cn.com.greendaoapp.bean.EventCenter;
import yang.cn.com.greendaoapp.bean.FileDao;
import yang.cn.com.greendaoapp.bean.FileInfo;
import yang.cn.com.greendaoapp.utils.FileUtil;
import yang.cn.com.greendaoapp.utils.SystemUtil;

/**
 * Created by Administrator on 2017/5/27.
 */

public class AllMainFragment extends BaseFragment {
    @Bind(R.id.imageView1)
    ImageView imageView1;
    @Bind(R.id.rl_mobile_memory)
    RelativeLayout rlMobileMemory;
    @Bind(R.id.imageView2)
    ImageView imageView2;
    @Bind(R.id.rl_sd_card)
    RelativeLayout rlSdCard;
    @Bind(R.id.imageView3)
    ImageView imageView3;
    @Bind(R.id.rl_extended_memory)
    RelativeLayout rlExtendedMemory;
    @Bind(R.id.tv_all_size)
    TextView tvAllSize;
    @Bind(R.id.tv_send)
    TextView tvSend;
    @Bind(R.id.activity_main)
    LinearLayout activityMain;

    @Override
    protected boolean isBindEventBusHere() {
        return true;
    }

    @Override
    protected void onEventComming(EventCenter var1) {

    }

    @Override
    public int getLayoutResource() {
        return R.layout.fragment_main_all;
    }

    @Override
    public void initView() {
        tvAllSize.setText(getString(R.string.size, "0B"));
        tvSend.setText(getString(R.string.send, "0"));
        SystemUtil.init(getActivity());
        updateSizAndCount();
    }

    @OnClick(R.id.rl_sd_card)
    void rl_sd_card() {
        if (checkSDEnvironment()) {
            Intent intent = new Intent(getActivity(), SDCardActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("path", Environment.getExternalStorageDirectory().getAbsolutePath());
            bundle.putString("name", "SD卡");
            intent.putExtras(bundle);
            startActivity(intent);
        } else {
            Alerter.create(getActivity())
                    .setTitle("通知")
                    .setText("您手机没有内置SD卡！")
                    .show();
        }
    }
    private boolean checkSDEnvironment() {
        boolean sdCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
        return sdCardExist;

    }

    public void updateSizAndCount() {
        List<FileInfo> mList = FileDao.queryAll();
        if (mList.size() == 0) {
            tvSend.setBackgroundResource(R.drawable.shape_bt_send);
           tvSend.setTextColor(getResources().getColor(R.color.md_grey_700));
            tvAllSize.setText(getString(R.string.size, "0B"));
        } else {
            tvSend.setBackgroundResource(R.drawable.shape_bt_send_blue);
            tvSend.setTextColor(getResources().getColor(R.color.md_white_1000));
            long count = 0L;
            for (int i = 0; i < mList.size(); i++) {
                count = count + mList.get(i).getFileSize();
            }
            tvAllSize.setText(getString(R.string.size, FileUtil.FormetFileSize(count)));
        }
        tvSend.setText(getString(R.string.send, "" + mList.size()));
    }

}
